package com.zx.config;

import com.zx.service.WrapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(WrapService.class)// 当classpath下发现该类的情况下进行自动配置。
@EnableConfigurationProperties(WrapProperties.class)// 为带有@ConfigurationProperties注解的Bean提供有效的支持
public class DemoAutoConfigure {

    @Autowired
    private WrapProperties wrapProperties;

    @Bean
    @ConditionalOnMissingBean //仅仅在当前上下文中不存在某个对象时，才会实例化一个Bean
    @ConditionalOnProperty(prefix = "demo.wrap", value = "enabled", havingValue = "true") // demo.wrap.enabled=true时。
    WrapService wrapService(){
        return new WrapService(wrapProperties.getPrefix(), wrapProperties.getSuffix());
    }
}
